package com.kingint.web.admin.serve.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.mbg.bas.entity.BasGoods;
import com.kingint.mbg.bas.entity.BasRoom;
import com.kingint.mbg.bas.mapper.BasRoomMapper;
import com.kingint.mbg.dto.BackQueryParam;
import com.kingint.mbg.dto.rushHouse.RushHouseBackParam;
import com.kingint.mbg.dto.rushHouse.RushHouseFrontParam;
import com.kingint.mbg.ser.entity.SerRushHouse;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerRushHouseMapper;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.mapper.UmsMemberMapper;
import com.kingint.web.admin.serve.service.SerRushHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.portal.serve.service.RushHouseService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.vo.RushBean;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 发布赶房表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2021-01-07
 */
@Service
@Slf4j
public class SerRushHouseServiceImpl extends ServiceImpl<SerRushHouseMapper, SerRushHouse> implements SerRushHouseService {
    @Autowired
    public RushHouseService serRushHouseService;
    @Resource
    private SerRushHouseMapper rushHouseMapper;
    @Resource
    private SerWorkOrderMapper workOrderMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private UmsMemberMapper memberMapper;
    @Resource
    private TaskService taskService;
    @Resource
    private BasRoomMapper roomMapper;
    @Resource
    private RushBean rushBean;

    @Override
    public int insertRushHouseInfo(RushHouseFrontParam serRushHouse) {
        return serRushHouseService.insertRushHouseInfo(serRushHouse);
    }

    @Override
    public int deleteRushHouseInfo(Integer id) {
        SerRushHouse serRushHouse = rushHouseMapper.selectById(id);
        Integer workId = serRushHouse.getWorkId();
        SerWorkOrder workOrder = new SerWorkOrder();
        workOrder.setId(workId);
        workOrder.setStatus(1);
        int i = workOrderMapper.updateById(workOrder);

        //同时查询是否有当前的流程实例，结束流程实例
        SerWorkOrder serWorkOrder = workOrderMapper.selectById(workId);
        String processInstanceId = serWorkOrder.getProcessInstanceId();
        runtimeService.deleteProcessInstance(processInstanceId, "赶房单总工单已经删除");

        //判断查房工单是否存在，存在就删除
        Integer checkWorkId = serRushHouse.getCheckWorkId();
        if(!StringUtils.isEmpty(checkWorkId)){
            SerWorkOrder order = new SerWorkOrder();
            order.setId(checkWorkId);
            order.setStatus(1);
            workOrderMapper.updateById(order);
            SerWorkOrder workOrder1 = workOrderMapper.selectById(checkWorkId);
            runtimeService.deleteProcessInstance(workOrder1.getProcessInstanceId(), "赶房单总工单已经删除");
        }
        return i;
    }

    @Override
    public IPage<RushHouseBackParam> getRushHouseList(BackQueryParam param, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号没有关联酒店id");
            return null;
        }
        //查询相关的赶房信息
        IPage<RushHouseBackParam> page = new Page<>(pageNum, pageSize);
        param.setHotelId(hotelId);
        IPage<RushHouseBackParam> backRushList = rushHouseMapper.getBackRushList(page, param);
        return backRushList;
    }

    @Override
    public SerRushHouse getRushHouseInfo(Integer id) {
        SerRushHouse serRushHouse = rushHouseMapper.selectById(id);
        //处理相关信息
        String arrangeContent = serRushHouse.getArrangeContent();
        if(!StringUtils.isEmpty(arrangeContent)){
            List<BasGoods> basGoods = JSONObject.parseArray(arrangeContent, BasGoods.class);
            serRushHouse.setArrangeList(basGoods);
        }
        Long checkUserId = serRushHouse.getCheckUserId();
        Long cleanUserId = serRushHouse.getCleanUserId();
        UmsMember umsMember = memberMapper.selectById(checkUserId);
        UmsMember umsMember1 = memberMapper.selectById(cleanUserId);
        if (!StringUtils.isEmpty(umsMember))
            serRushHouse.setCheckUserName(umsMember.getUsername());
        if (!StringUtils.isEmpty(umsMember1))
            serRushHouse.setCleanUserName(umsMember1.getUsername());
        SerWorkOrder serWorkOrder1 = workOrderMapper.selectById(serRushHouse.getWorkId());
        BasRoom basRoom = roomMapper.selectById(serWorkOrder1.getRoomId());//房间信息
        serRushHouse.setRoomNum(basRoom.getRoomNum());
        serRushHouse.setOrderSn(serWorkOrder1.getOrderSn());//工单号
        //查询查房状态是否完成
        Integer checkWorkId = serRushHouse.getCheckWorkId();//查询查房工单
        if (!StringUtils.isEmpty(checkWorkId)) {
            SerWorkOrder serWorkOrder = workOrderMapper.selectById(checkWorkId);
            if (StringUtils.isEmpty(serWorkOrder)) {
                serRushHouse.setCheckStatus(0);//未完成
            } else {
                Task task = taskService.createTaskQuery()
                        .processInstanceId(serWorkOrder.getProcessInstanceId())
                        .singleResult();
                if (StringUtils.isEmpty(task)) {
                    serRushHouse.setCheckStatus(1);//完成
                } else {
                    serRushHouse.setCheckStatus(0);//未完成
                }
            }
        } else {
            serRushHouse.setCheckStatus(0);//未完成
        }
        return serRushHouse;
    }

    @Override
    public int completeRushHouseInfo(Integer status, Integer id) {
        SerWorkOrder serWorkOrder = workOrderMapper.selectById(id);
        Task task = taskService.createTaskQuery()
                .processInstanceId(serWorkOrder.getProcessInstanceId())
                .singleResult();
        if(!StringUtils.isEmpty(task)){
            Map<String,Object> map=new HashMap<>();
            map.put(rushBean.getRejectStatus(),status);
            taskService.setVariables(task.getId(),map);
            taskService.complete(task.getId());
            return 1;
        }
        return 0;
    }
}
